package biz

import (
	"time"

	"github.com/gin-gonic/gin"
	"gitlab1.moolink.net/trade/backend/gen"
	"lab.com/conf"
	"lab.com/consts"
	"lab.com/gen/entity"
	"lab.com/request"
)

type iurBiz struct {
}

// Iur  产学研
var Iur = iurBiz{}

// List 产学研列表
func (iur *iurBiz) List(c *gin.Context, req request.IurListReq) (out request.IurListRes, err error) {
	ctx := conf.Span(c, "biz iur list", nil)
	iurModel := conf.LabGen.Iur

	where := []gen.Condition{iurModel.Status.Eq(consts.IurNormal), iurModel.IsShow.Eq(consts.IurShow)}
	// if req.IsAll == consts.IurInHome {
	// 	where = append(where, iurModel.IsHome.Eq(req.IsAll))
	// }
	if req.Title != "" {
		where = append(where, iurModel.Title.Like("%"+req.Title+"%"))
	}
	if req.StartTime != "" {
		if t, err := time.Parse("2006-01-02", req.StartTime); err != nil {
			return out, err
		} else {
			where = append(where, iurModel.EstablishTime.Gte(t))
		}
	}
	if req.EndTime != "" {
		if t, err := time.Parse("2006-01-02", req.EndTime); err != nil {
			return out, err
		} else {
			where = append(where, iurModel.EstablishTime.Lte(t))
		}
	}

	info, count, err := iurModel.WithContext(ctx).Where(where...).Order(iurModel.EstablishTime).FindByPage(req.Offset, req.Size)
	if err != nil {
		return out, err
	}

	list := make([]request.IurBasicInfo, 0, len(info))
	for _, val := range info {
		list = append(list, request.IurBasicInfo{
			Id:            val.ID,
			Title:         val.Title,
			Abstract:      val.Abstract,
			Author:        val.Author,
			ImgPath:       val.ImgPath,
			EstablishTime: val.EstablishTime.Format("2006-01-02"),
		})
	}
	out.List = list
	out.Count = int(count)
	return out, nil
}

// Detail 产学研详情
func (iur *iurBiz) Detail(c *gin.Context, req request.IurDetailReq) (out request.IurDetailRes, err error) {
	ctx := conf.Span(c, "biz iur detail", nil)
	iurModel := conf.LabGen.Iur

	info, err := iurModel.WithContext(ctx).Where(iurModel.ID.Eq(req.ID)).First()
	if err != nil {
		return out, err
	}

	out.Content = info.Content
	out.Id = info.ID
	out.Title = info.Title
	out.Abstract = info.Abstract
	out.Author = info.Author
	out.ImgPath = info.ImgPath
	out.EstablishTime = info.EstablishTime.Format("2006-01-02")
	//out.IsHome = info.IsHome
	out.IsShow = info.IsShow
	return out, nil
}

// Add 产学研新增
func (iur *iurBiz) Add(c *gin.Context, req request.IurAddReq) (err error) {
	ctx := conf.Span(c, "biz iur add", nil)
	iurModel := conf.LabGen.Iur

	t, err := time.Parse("2006-01-02", req.EstablishTime)
	if err != nil {
		return err
	}

	info := &entity.Iur{
		Title:         req.Title,
		Abstract:      req.Abstract,
		Author:        req.Author,
		Content:       req.Content,
		EstablishTime: t,
		IsShow:        req.IsShow,
		//IsHome:        req.IsHome,
		ImgPath:    req.ImgPath,
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}

	err = iurModel.WithContext(ctx).Create(info)
	return err
}

// Delete 产学研删除
func (iur *iurBiz) Delete(c *gin.Context, req request.IurDeleteReq) (err error) {
	ctx := conf.Span(c, "biz iur delete", nil)
	iurModel := conf.LabGen.Iur

	info := &entity.Iur{
		Status:     consts.IurDelete,
		UpdateTime: time.Now(),
	}

	_, err = iurModel.WithContext(ctx).Select(iurModel.UpdateTime, iurModel.Status).Where(iurModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Edit 产学研编辑
func (iur *iurBiz) Edit(c *gin.Context, req request.IurEditReq) (err error) {
	ctx := conf.Span(c, "biz iur edit", nil)
	iurModel := conf.LabGen.Iur

	t, err := time.Parse("2006-01-02", req.EstablishTime)
	if err != nil {
		return err
	}

	info := &entity.Iur{
		Title:         req.Title,
		Abstract:      req.Abstract,
		Author:        req.Author,
		Content:       req.Content,
		EstablishTime: t,
		IsShow:        req.IsShow,
		//IsHome:        req.IsHome,
		ImgPath:    req.ImgPath,
		UpdateTime: time.Now(),
	}

	_, err = iurModel.WithContext(ctx).Select(iurModel.Title, iurModel.Abstract, iurModel.Author, iurModel.Content,
		iurModel.EstablishTime, iurModel.IsShow, iurModel.ImgPath, iurModel.UpdateTime).
		Where(iurModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Show 产学研展示
func (iur *iurBiz) Show(c *gin.Context, req request.IurShowReq) (err error) {
	ctx := conf.Span(c, "biz iur show", nil)
	iurModel := conf.LabGen.Iur

	info := &entity.Iur{
		IsShow:     req.IsShow,
		UpdateTime: time.Now(),
	}

	_, err = iurModel.WithContext(ctx).Select(iurModel.UpdateTime, iurModel.IsShow).Where(iurModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Home 产学研首页展示
// func (iur *iurBiz) Home(c *gin.Context, req request.IurHomeReq) (err error) {
// 	ctx := conf.Span(c, "biz iur home", nil)
// 	iurModel := conf.LabGen.Iur

// 	if count, err := iurModel.WithContext(ctx).Where(iurModel.IsHome.Eq(consts.IurInHome), iurModel.IsShow.Eq(consts.IurShow),
// 		iurModel.Status.Eq(consts.IurNormal)).Count(); err != nil {
// 		return err
// 	} else if count > 8 {
// 		return errorcode.New(errorcode.ErrService, "首页展示个数超过限制！", nil)
// 	}

// 	info := &entity.Iur{
// 		IsHome:     req.IsHome,
// 		UpdateTime: time.Now(),
// 	}

// 	_, err = iurModel.WithContext(ctx).Select(iurModel.UpdateTime, iurModel.IsHome).Where(iurModel.ID.Eq(req.ID)).Updates(info)
// 	return err
// }

// List 产学研列表
func (iur *iurBiz) ListInManage(c *gin.Context, req request.IurListInManageReq) (out request.IurListInManageRes, err error) {
	ctx := conf.Span(c, "biz iur list in manage", nil)
	iurModel := conf.LabGen.Iur

	where := []gen.Condition{iurModel.Status.Eq(consts.IurNormal)}
	if req.Title != "" {
		where = append(where, iurModel.Title.Like("%"+req.Title+"%"))
	}
	if req.StartTime != "" {
		if t, err := time.Parse("2006-01-02", req.StartTime); err != nil {
			return out, err
		} else {
			where = append(where, iurModel.EstablishTime.Gte(t))
		}
	}
	if req.EndTime != "" {
		if t, err := time.Parse("2006-01-02", req.EndTime); err != nil {
			return out, err
		} else {
			where = append(where, iurModel.EstablishTime.Lte(t))
		}
	}

	info, count, err := iurModel.WithContext(ctx).Where(where...).Order(iurModel.EstablishTime.Desc()).FindByPage(req.Offset, req.Size)
	if err != nil {
		return out, err
	}

	list := make([]request.IurInfo, 0, len(info))
	for _, val := range info {
		list = append(list, request.IurInfo{
			Id:            val.ID,
			Title:         val.Title,
			Abstract:      val.Abstract,
			Author:        val.Author,
			ImgPath:       val.ImgPath,
			EstablishTime: val.EstablishTime.Format("2006-01-02"),
			//IsHome:        val.IsHome,
			IsShow: val.IsShow,
		})
	}
	out.List = list
	out.Count = int(count)
	return out, nil
}
